package com.leji.song.util;

import android.content.Context;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
 * Created by colin.ding on 2014/6/16.
 */
public class BaiduMapUtil {

    public static String myAdress = "";

    private LocationClient mLocationClient;

    public interface GetLocationCallback{

        void onLocation(String locationString);
    }


    private GetLocationCallback getLocationCallback;

    public BaiduMapUtil(Context context, GetLocationCallback getLocationCallback){
        this.getLocationCallback = getLocationCallback;
        mLocationClient = new LocationClient(context.getApplicationContext());
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);                                //打开gps
        option.setCoorType("bd09ll");                           //设置坐标类型为bd09ll
        option.setPriority(LocationClientOption.NetWorkFirst);  //设置网络优先
        option.setProdName("locSDKDemo2");                      //设置产品线名称
        option.setScanSpan(1000*60*5);                               //定时定位，每隔5分钟定位一次。
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);

        mLocationClient.registerLocationListener(new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
                if (location == null)
                    return ;
                StringBuffer sb = new StringBuffer(256);
                sb.append("time : ");
                sb.append(location.getTime());
                sb.append("\nerror code : ");
                sb.append(location.getLocType());
                sb.append("\nlatitude : ");
                sb.append(location.getLatitude());
                sb.append("\nlontitude : ");
                sb.append(location.getLongitude());
                sb.append("\nradius : ");
                sb.append(location.getRadius());
                if (location.getLocType() == BDLocation.TypeGpsLocation){
                    sb.append("\nspeed : ");
                    sb.append(location.getSpeed());
                    sb.append("\nsatellite : ");
                    sb.append(location.getSatelliteNumber());
                } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                    sb.append("\naddr : ");
                    sb.append(location.getAddrStr());
                }
                sb.append("\nsdk version : ");
                sb.append(mLocationClient.getVersion());

                myAdress = location.getAddrStr();

                BaiduMapUtil.this.getLocationCallback.onLocation(location.getAddrStr());
            }

            public void onReceivePoi(BDLocation location){
                //return ;
            }
        });

    }

    public void start(){
        mLocationClient.start();
    }


    public void stop(){
        mLocationClient.stop();
    }


}
